package com.josehp.demo;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloScheduler {
	public static void main(String[] args) throws SchedulerException, InterruptedException {
		//创建一个JobDetail实例    与hellojob.class绑定
		Date date = new Date();
		SimpleDateFormat sf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println("Current Time Is :"+sf.format(date));
		JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
				.build();
		//每秒钟触发一次任务
		CronTrigger trigger = (CronTrigger)TriggerBuilder
				.newTrigger()
				.withIdentity("myTrigger","group1")
				.withSchedule(CronScheduleBuilder.
						cronSchedule("* * * * * ?"))
				.build();
		//创建Scheduler实例
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		scheduler.start();
		System.out.println("scheduled time is :"+sf.format(scheduler.scheduleJob(jobDetail,trigger)));
		//scheduler 执行两秒后挂起
		Thread.sleep(2000L);
	//	scheduler.standby();
		//scheduler.shutdown(true)表示等待所有正在执行的job执行完毕后，再关闭scheduler
		//scheduler.shutdown(false)表示直接关闭scheduler  默认
		scheduler.shutdown(true);//
		//挂起后3秒后继续执行
//		Thread.sleep(3000L);
//		scheduler.start();
		System.out.println("scheduler is shut down?"+scheduler.isShutdown());
	}
}
